#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <signal.h>

/* // signal函数
void handler(int sig)
{
    std::cout << "捕捉到一个信号，编号是: " << sig << std::endl;
}

int main()
{
    signal(SIGINT, handler);
    while (true)
    {
        std::cout << "hello world, my pid is " << getpid() << std::endl;
        sleep(1);
    }

    return 0;
}
 */


/* // 模拟kill函数
void Usage(const std::string& str)
{
    std::cout << "using warning " << std::endl;
    std::cout << "Please enter: " << str << " pid sign" << std::endl;
}

int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(1);
    }
    int pid = atoi(argv[1]);
    int sign = atoi(argv[2]);
    kill(pid, sign);

    return 0;
} */


/* // raise函数
void handler(int sig)
{
    std::cout << "捕捉到一个信号，编号是: " << sig << std::endl;
}

int main()
{
    signal(SIGINT, handler);
    int cnt = 5;
    while (true)
    {
        std::cout << "hello raise " << cnt-- << std::endl;
        sleep(1);
        if (cnt == 0)
        {
            raise(SIGINT);
            //kill(getpid(), SIGINT);
            cnt = 5;
        }
    }

    return 0;
} */


//常见异常信号
/* void handler(int sig)
{
    std::cout << "捕捉到一个信号，编号是: " << sig << std::endl;
    sleep(1);
}

int main()
{
    signal(SIGFPE, handler);
    while (true)
    {
        std::cout << "除0错误,发送8号信号" << std::endl;
        int a = 5;
        int b = a / 0;
        sleep(1);
    }

    return 0;
}
//CPU状态寄存器0变成1，但是操作系统识别到后，会向目标进程不停发送8号信号 */



/* //计算一秒内CPU计算次数
long long cnt = 0;

void handler(int sig)
{
    std::cout << "捕捉到一个信号，编号是: " << sig << std::endl;
    std::cout << "cnt: " << cnt << std::endl;
    alarm(1);
    cnt = 0;
}

int main()
{
    signal(SIGALRM, handler);
    alarm(1);
    while (true)
    {
        cnt++;
    }

    return 0;
} */

/* void handler(int sig)
{
    std::cout << "捕捉到一个信号，编号是: " << sig << std::endl;
    sleep(1);
}

void Show_Pending(const sigset_t& pending)
{
    for (int i = 1; i < 32; i++)
    {
        if (sigismember(&pending, i) == 1)
            std::cout << 1;
        else 
            std::cout << 0;
    }
    std::cout << std::endl;
}

int main()
{
    for (int i = 1; i < 32; i++)
    {
        signal(i, handler);
    }

    sigset_t Block;
    sigset_t oldBlock;
    sigset_t Pending;

    sigemptyset(&Block);
    sigemptyset(&oldBlock);

    for (int i = 1; i < 32; i++)
    {
        sigaddset(&Block, i);
    }

    sigprocmask(SIG_SETMASK, &Block, &oldBlock);

    int cnt = 1;
    while (true)
    {
        sigpending(&Pending);
        Show_Pending(Pending);
        
        if (cnt < 32 && cnt != 9 && cnt != 19)
            kill(getpid(), cnt);
        cnt++;

        if (cnt == 40)
        {
            sigprocmask(SIG_SETMASK, &oldBlock, &Block);
        }

        sleep(1);
    }

    return 0;
} */

void handler(int sig)
{
    std::cout << "捕捉到一个信号，编号是: " << sig << std::endl;
    sleep(5);
}

int main()
{
    struct sigaction act;
    struct sigaction oact;

    act.sa_flags = 0;
    act.sa_handler = handler;
    sigemptyset(&act.sa_mask);

    sigaction(SIGINT, &act, &oact);
    while (true)
    {
        std::cout << "hello sigaction" << std::endl;
        sleep(1);
    }

    return 0;
}